const SortingAlgorithms = require("../template")

function testSortingEdgeCases() {
  const sorter = new SortingAlgorithms()

  console.log(
    "1. 空数组测试:",
    (() => {
      const emptyArray = []
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => {
        const result = sorter[algo]([...emptyArray])
        return Array.isArray(result) && result.length === 0
      })
    })()
  )

  console.log(
    "2. 单元素数组测试:",
    (() => {
      const singleElement = [42]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => JSON.stringify(sorter[algo]([...singleElement])) === JSON.stringify([42]))
    })()
  )

  console.log(
    "3. 两元素数组测试:",
    (() => {
      const twoElements = [2, 1]
      const expectedTwo = [1, 2]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => JSON.stringify(sorter[algo]([...twoElements])) === JSON.stringify(expectedTwo))
    })()
  )

  console.log(
    "4. 已排序数组测试:",
    (() => {
      const sortedArray = [1, 2, 3, 4, 5]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => JSON.stringify(sorter[algo]([...sortedArray])) === JSON.stringify([1, 2, 3, 4, 5]))
    })()
  )

  console.log(
    "5. 逆序数组测试:",
    (() => {
      const reverseArray = [5, 4, 3, 2, 1]
      const expectedReverse = [1, 2, 3, 4, 5]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => JSON.stringify(sorter[algo]([...reverseArray])) === JSON.stringify(expectedReverse))
    })()
  )

  console.log(
    "6. 相同元素数组测试:",
    (() => {
      const sameElements = [3, 3, 3, 3, 3]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => JSON.stringify(sorter[algo]([...sameElements])) === JSON.stringify([3, 3, 3, 3, 3]))
    })()
  )

  console.log(
    "7. 重复元素数组测试:",
    (() => {
      const duplicateArray = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
      const expectedDuplicate = [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => JSON.stringify(sorter[algo]([...duplicateArray])) === JSON.stringify(expectedDuplicate))
    })()
  )

  console.log(
    "8. 包含零的数组测试:",
    (() => {
      const zeroArray = [0, 5, 0, 3, 0, 1]
      const expectedZero = [0, 0, 0, 1, 3, 5]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => JSON.stringify(sorter[algo]([...zeroArray])) === JSON.stringify(expectedZero))
    })()
  )

  console.log(
    "9. 包含负数的数组测试:",
    (() => {
      const negativeArray = [-3, 1, -4, 2, -5]
      const expectedNegative = [-5, -4, -3, 1, 2]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => JSON.stringify(sorter[algo]([...negativeArray])) === JSON.stringify(expectedNegative))
    })()
  )

  console.log(
    "10. this上下文边界测试:",
    (() => {
      const edgeObj = {
        multiplier: 2,
        processEdgeCase: function(arr) {
          const sorter = new SortingAlgorithms()
          const result = sorter.bubbleSort([...arr])
          return result.map(item => item * this.multiplier)
        }
      }
      const result = edgeObj.processEdgeCase([2, 1])
      return JSON.stringify(result) === JSON.stringify([2, 4])
    })()
  )

  console.log(
    "11. 复杂对象this绑定边界测试:",
    (() => {
      const edgeConfigObj = {
        config: { offset: 10 },
        processWithConfig: function(arr) {
          const sorter = new SortingAlgorithms()
          const result = sorter.selectionSort([...arr])
          return result.map(item => item + this.config.offset)
        }
      }
      const result = edgeConfigObj.processWithConfig([1])
      return JSON.stringify(result) === JSON.stringify([11])
    })()
  )
}

testSortingEdgeCases()
